from rest_framework import status
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from django.contrib.auth.models import Group
from meiduo_admin.admin.admin_serializers import AdminModelSerializer, AdminGroupSerializer
from meiduo_admin.user.my_pagination import MyNumberPagination
from users.models import User


# 获取所有管理员
class AdminModelViewSet(ModelViewSet):
    pagination_class = MyNumberPagination
    queryset = User.objects.filter(is_staff=True).all()
    serializer_class = AdminModelSerializer

    def destroy(self, request, *args, **kwargs):
        user = self.get_object()
        if user == self.request.user:
            return Response({"essmsg": "不能删除自己"}, status=status.HTTP_400_BAD_REQUEST)
        return super(AdminModelViewSet, self).destroy(request, *args, **kwargs)


# 获取用户组
class AdminGroupViewSet(ListAPIView):
    pagination_class = None
    queryset = Group.objects.all()
    serializer_class = AdminGroupSerializer
